﻿/// <reference path="jquery-1.8.0.intellisense.js" />
/// <reference path="../knockout-2.1.0.debug.js" />
/// <reference path="../knockout.mapping-latest.js" />
/// <reference path="expenses.repository.js" />

//START validation configuration
ko.validation.rules.pattern.message = 'Invalid.';

ko.validation.configure({
    registerExtenders: true,
    messagesOnModified: true,
    insertMessages: false,
    parseInputAttributes: true,
    messageTemplate: null,
    decorateElement: true
});

validationOptions: { errorElementClass: 'input-validation-error' };

//END validation configuration



//------------ Custom bindings  ----------
ko.bindingHandlers.edDateText = {
    init: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
        var date = new Date(valueAccessor()());
        $(element).text(formatDate(date));
    },
    update: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
        var date = new Date(valueAccessor()());
        $(element).text(formatDate(date));
    }
};


function formatDate(date) {
    return (date.getMonth() + 1) + "/" + date.getDate() + "/" + date.getFullYear();
}
//------------ Custom bindings  ----------


function generateViewModel(viewModelJs) {

    viewModelJs.expenseToAdd = {
        
        title: "",
        description: "",
        cost: "",
        date: ""
    };

    viewModelJs.expenseToEdit = {
        expenseRecordId: "",
        title: "",
        description: "",
        cost: "",
        date: ""
    };

    var viewModel = ko.mapping.fromJS(viewModelJs);

    //------- Computed fields ---------
    viewModel.expensesSum = ko.computed(
        {
            read: function () {
                var sum = 0;
                for (var i in viewModel.expenses()) {
                    sum += parseFloat(viewModel.expenses()[i].cost());
                }
                return sum;
            }
        });

    //viewModel.expenseToAdd.cost.extend({ min: 0 });
    //viewModel.expenseToEdit.cost.extend({ min: 0 });
    
    //------- Computed fields ---------
    


    //------- Observable fields ---------
    
    

    //------- Observable fields ---------

    //------- Functions ---------

    viewModel.removeExpense = function () {
        deleteExpense(this, viewModel);
    }

    viewModel.editExpense = function () {
        
        viewModel.expenseToEdit.expenseRecordId(this.expenseRecordId());
        viewModel.expenseToEdit.title(this.title());
        viewModel.expenseToEdit.description(this.description());
        viewModel.expenseToEdit.cost(this.cost());

        var d = new Date(this.date());
        viewModel.expenseToEdit.date(formatDate(d));
       
    }

    viewModel.cancelEdit = clearExpense(viewModel.expenseToEdit);

    viewModel.saveExpense = function () {
        
        putUpdateExpense(viewModel.expenseToEdit, viewModel);
        
    }

    viewModel.addExpense = function () {
        postExpense(viewModel.expenseToAdd, viewModel);
    };

    //------- Functions ---------

    return viewModel;

}